﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.Base;
using SmartWMS.Domain.FinanceManage;
using SmartWMS.Server.FinanceManage.PaymentService;
using SmartWMS.Server.FinanceManage.PaymentService.MeetDto;
using SmartWMS.Server.FinanceManage.PayTypeServices.Dto;
using SmartWMS.Server.Repository;

namespace SmartWMS.Server.FinanceManage.PayTypeServices
{
	/// <summary>
	/// 应付管理
	/// </summary>
	public class MeetService : IMeetService
	{
		private readonly IRepository<Payment> _meet;

		private readonly IRepository<PayType> _paytype;

		public MeetService(IRepository<Payment> meet, IRepository<PayType> paytype)
		{
			_meet = meet;
			_paytype = paytype;
		}

		/// <summary>
		/// 添加应付信息
		/// </summary>
		/// <param name="dto">产品Dto</param>
		/// <returns></returns>
		public async Task<Response<bool>> AddMeetAsync(meetsDto dto)
		{
			dto.CreateTime = DateTime.Now;
			dto.Opinion = "暂无意见";
			dto.Reviewer = "刘德华";
			dto.Audittime = DateTime.Now;
			dto.PayInstitution = "银行";
			dto.State = 1;
			var meet = dto.MapTo<Payment>();
			var result = await _meet.InsertAsync(meet);
			return result > 0 ? Response<bool>.Ok(result > 0) :
				 Response<bool>.Fail(result > 0);
		}

		/// <summary>
		/// 加载支付类别
		/// </summary>
		/// <returns></returns>
		public async Task<Response<List<PayType>>> BindPayTypeAsync()
		{
			var type = _paytype.GetAllListAsync();
			return Response<List<PayType>>.Ok(await type);
		}

		/// <summary>
		/// 应付列表
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public async Task<Response<List<meetsDto>>> ShowMeetAsync(queryDto dto)
		{
			var meet = await _meet.GetAllListAsync();
			var type = await _paytype.GetAllListAsync();

			var list = (from a in meet
						join b in type on a.TypeId equals b.Id
						where (string.IsNullOrEmpty(dto.SerialNumber) || a.SerialNumber.Contains(dto.SerialNumber))
						   && (string.IsNullOrEmpty(dto.Name) || a.Name.Contains(dto.Name))
						   && (string.IsNullOrEmpty(dto.Payee) || a.Payee.Contains(dto.Payee))
						   && (dto.TypeId == 0 || a.TypeId == dto.TypeId)
						   && (dto.State == 0 || a.State == dto.State)
						   && (dto.start <= a.CreateTime || dto.start == null)
						   && (dto.end >= a.CreateTime.AddDays(-1) || dto.end == null)
						select new meetsDto
						{
							Id = a.Id,
							SerialNumber = a.SerialNumber,
							Name = a.Name,
							TypeName = b.TypeName,
							Payee = a.Payee,
							NetReceipts = a.NetReceipts,
							Payer = a.Payee,
							Payable = a.Payable,
							PayInstitution = a.PayInstitution,
							LastPaymentDate = (DateTime)a.LastPaymentDate,
							CreateTime = a.CreateTime,
							State = a.State,
						}).Distinct().ToList();
			int totalCount = list.Count();
			int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.pageSize);
			list = list.Skip((dto.pageIndex - 1) * dto.pageSize).Take(dto.pageSize).ToList();
			var result = list.MapToList<meetsDto>();
			return result != null ? Response<List<meetsDto>>.Ok(result, totalCount) : Response<List<meetsDto>>.Fail("查询失败");
		}

		/// <summary>
		/// 删除报损信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<Response<bool>> DeletePaymentAsync(KeyEntity id)
		{
			var resault = id.MapTo<Payment>();
			var deletelist = await _meet.DeleteByIdAsync(resault);
			return deletelist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
		}

		/// <summary>
		/// 批量删除报损信息
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public async Task<Response<bool>> BatchDelPaymentAsync(string id)
		{
			var ids = id.Split(',');
			var list = new List<Payment>();
			foreach (var item in ids)
			{
				var pList = _meet.GetListQuery().Where(t => t.Id == int.Parse(item));
				list.AddRange(pList);
			}
			var result = await _meet.DeleteAsync(list);
			return result > 0 ? Response<bool>.Ok(result > 0) :
				 Response<bool>.Fail(result > 0);
		}

		/// <summary>
		/// 反填、查看应付信息
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public Response<meetsDto> ReversePaymentAsync(meetsDto dto)
		{
			var meet = _meet.GetListQuery().FirstOrDefault(x => x.Id == dto.Id);
			var resault = meet.MapTo<meetsDto>();
			return Response<meetsDto>.Ok(resault);
		}

		/// <summary>
		/// 修改产品信息
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public async Task<Response<bool>> UpdatePaymentAsync(meetsDto dto)
		{
			var update = dto.MapTo<Payment>();
			var resault = await _meet.UpdateAsync(update);
			return resault > 0 ? Response<bool>.Ok(resault > 0) : Response<bool>.Fail(resault > 0);
		}

		/// <summary>
		/// 修改审核状态
		/// </summary>
		/// <param name="dto"></param>
		/// <returns></returns>
		public async Task<Response<bool>> UpdateStateAsync(meetsDto dto)
		{
			var meet = dto.MapTo<Payment>();
			if (dto.State == 1 || dto.State == 3)
			{
				meet.State = 2;
			}
			var result = await _meet.UpdateAsync(meet);
			return result > 0 ? Response<bool>.Ok(result > 0) :
				  Response<bool>.Fail(result > 0);
		}
	}
}
